package myky.com.doctorapp.ui

import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.gyf.barlibrary.ImmersionBar
import kotlinx.android.synthetic.main.activity_find_doctor_by_area.*
import myky.com.doctorapp.R
import myky.com.doctorapp.adapter.FindDoctorCityAdapter
import myky.com.doctorapp.adapter.FindDoctorProvinceAdapter
import myky.com.doctorapp.base.BaseActivity
import myky.com.doctorapp.contract.FindDoctorByAreaContract
import myky.com.doctorapp.entity.AreaWrapperEntity
import myky.com.doctorapp.presenter.FindDoctorByAreaPresenter

class FindDoctorByAreaActivity : BaseActivity<FindDoctorByAreaPresenter, AreaWrapperEntity>(), FindDoctorByAreaContract.View, BaseQuickAdapter.OnItemChildClickListener {
    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {

        val data = provinceAdapter.data

        for ((index, value) in data.withIndex()) {
            value.isSelected = index == position
        }


        val tempCitys = provinceAdapter.getItem(position)?.city
        cityList.clear()

        tempCitys?.let {
            cityList.addAll(it)
        }
        provinceAdapter.notifyDataSetChanged()
        cityAdapter.notifyDataSetChanged()

    }

    private lateinit var provinces: ArrayList<AreaWrapperEntity.DataBean.AreaBean>
    private lateinit var cityList: ArrayList<AreaWrapperEntity.DataBean.AreaBean.CityBean>

    private lateinit var provinceAdapter: FindDoctorProvinceAdapter
    private lateinit var cityAdapter: FindDoctorCityAdapter

    override fun reloadActivity() {
    }

    override fun injectTarget(): View = findViewById(R.id.injectView)

    override fun initData() {
        mPresenter.findArea()
        provinceAdapter.onItemChildClickListener = this
    }

    override fun initView() {
        my_title_tv1.text = "按地区找医生"
        ImmersionBar.with(this)
                .statusBarColor(R.color.colorPrimary)
                .titleBar(R.id.my_self_title)  //使用该属性必须指定状态栏的颜色，不然状态栏透明，很难看
                .init()
        provinces = ArrayList()
        cityList = ArrayList()
        province.layoutManager = LinearLayoutManager(this)
        cityName.layoutManager = LinearLayoutManager(this)
        provinceAdapter = FindDoctorProvinceAdapter(provinces)
        cityAdapter = FindDoctorCityAdapter(cityList)
        province.adapter = provinceAdapter
        cityName.adapter = cityAdapter
        backLayout.setOnClickListener { finish() }

    }

    override fun getContenView(): Int = R.layout.activity_find_doctor_by_area

    override fun showDataSuccess(datas: AreaWrapperEntity?) {
    }

    override fun showArra(areaEntities: List<AreaWrapperEntity.DataBean.AreaBean>) {

        provinces.addAll(areaEntities)

        provinceAdapter.notifyDataSetChanged()

        cityList.addAll(provinces[0].city)

        cityAdapter.notifyDataSetChanged()
    }

    override fun getPresenter(): FindDoctorByAreaPresenter = FindDoctorByAreaPresenter()
}
